package com.atguigu.gulimall.member.service.impl;

import com.atguigu.common.vo.SocialUser;
import com.atguigu.gulimall.member.entity.MemberLevelEntity;
import com.atguigu.gulimall.member.exception.PhoneExistException;
import com.atguigu.gulimall.member.exception.UsernameExistException;
import com.atguigu.gulimall.member.service.MemberLevelService;
import com.atguigu.gulimall.member.vo.MemberLoginVo;
import com.atguigu.gulimall.member.vo.UserRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    MemberLevelService levelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(UserRegistVo user) {
        checkUserNameUnique(user.getUserName());
        checkPhoneUnique(user.getPhone());

        MemberEntity member = new MemberEntity();
        MemberLevelEntity defaultLevel = levelService.getDefaultLevel();
        member.setLevelId(defaultLevel.getId());
        member.setMobile(user.getPhone());
        member.setUsername(user.getUserName());
        member.setNickname(user.getUserName());

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(user.getPassword());
        member.setPassword(encode);

        this.baseMapper.insert(member);
    }

    @Override
    public void checkUserNameUnique(String username) throws UsernameExistException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        if (count > 0) {
            throw new UsernameExistException();
        }
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (count > 0) {
            throw new PhoneExistException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo member) {
        String acct = member.getLoginacct();
        MemberEntity entity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", acct).or().eq("mobile", acct));
        if (entity == null) {
            return null;
        } else {
            String password = member.getPassword();
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String enpassword = entity.getPassword();
            boolean match = encoder.matches(password, enpassword);
            if (match) {
                return entity;
            } else {
                return null;
            }
        }
    }

    @Override
    public MemberEntity login(SocialUser socialUser) {
        String uid = socialUser.getSocialUid();

        MemberEntity member = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", uid));
        if (member != null) {
            MemberEntity update = new MemberEntity();
            update.setId(member.getId());
            update.setAccessToken(socialUser.getAccessToken());
            update.setExpiresIn(socialUser.getExpiresIn());

            member.setAccessToken(socialUser.getAccessToken());
            member.setExpiresIn(socialUser.getExpiresIn());

            return member;
        } else {
            MemberEntity regist = new MemberEntity();
            regist.setSocialUid(uid);
            regist.setAccessToken(socialUser.getAccessToken());
            regist.setExpiresIn(socialUser.getExpiresIn());

            if ("gitee".equals(socialUser.getSocialType())) {
                regist.setUsername(socialUser.getName());
                regist.setNickname(socialUser.getName());
            }
            this.baseMapper.insert(regist);
            return regist;
        }
    }

}